Visual Basic .NET offers a non-short-circuit conditional function, IIf()
, which returns either its second or third parameter based on
the expression in the first parameter. Using it is slower than using If()
because each parameter is unconditionally evaluated. Further,
its use can lead to runtime exceptions because IIf
always evaluates all three of its arguments.
The newer version, If()
, should be used instead because it short-circuits the evaluation of its parameters.
Noncompliant code example
Public Class Foo
Public Sub Bar()
Dim var As Object = IIf(Date.Now.Year = 1999, "Lets party!", "Lets party like it is 1999!") ' Noncompliant
End Sub
End Class
Compliant solution
Public Class Foo
Public Sub Bar()
Dim var As String = If(Date.Now.Year = 1999, "Lets party!", "Lets party like it is 1999!")
End Sub
End Class